#define LOOT_DONE 0
#define LOOT_MOVE 1
#define LOOT_WAIT 2
#define LOOT_START 3
#define LOOT_LOOT 4

#define LOOT_DISTANCE 5

objectdef Looting inherits BotBase
{
	variable string LootObject
	variable string LastLooted
	variable bool FullInventory = FALSE
	variable int LootIndex = 1
	variable bool DoneLooting = TRUE
	variable int LootStart = ${LavishScript.RunningTime}
	variable int LastAction = ${LavishScript.RunningTime}
	
	method LootCorpse(string CorpseID)
	{
		This:EA["Looting corpse: ${Spawn[${CorpseID}].Name}"]
		This:Loot[${CorpseID}]
	}
	 
	method Loot(string LootID)
	{
		This.LootStart:Set[${LavishScript.RunningTime}]
		This.LootObject:Set[${LootID}]
		This.DoneLooting:Set[FALSE]
	}
	
	method Reset()
	{
		This.LootObject:Set[0]
		This.DoneLooting:Set[TRUE]
	}

	member ValidLoot()
	{
		if ${Spawn[${This.LootObject}].X} != 0 && ${Spawn[${This.LootObject}].Y} != 0
		{
			return TRUE
		}
		return FALSE
	}
	
	member Done()
	{
		if !${This.DoneLooting}
		{
			return FALSE
		}
		return TRUE
	}
	
	member State()
	{
		if !${Spawn[${This.LootObject}](exists)}
		{
			return LOOT_DONE
		}
		if !${Move.HaveDestination} && ${Spawn[${This.LootObject}].Distance} >= LOOT_DISTANCE
		{
			return LOOT_MOVE
		}
		
		if ${Move.HaveDestination} && ${Spawn[${This.LootObject}].Distance} >= LOOT_DISTANCE
		{
			return LOOT_WAIT
		}

		if ${Math.Calc[${LavishScript.RunningTime} - ${This.LastAction}]} < 1000
		{
			return LOOT_WAIT
		}

		if ${Window[LootWnd]}
		{
			return LOOT_LOOT
		}

		if ${Spawn[${This.LootObject}].Distance} <= LOOT_DISTANCE
		{
			return LOOT_START
		}
		
		return LOOT_DONE
	}
	
	method Pulse()
	{
		This.CurrentState:Set[${This.State}]
		;This:EA["${This.State}"]
		switch ${This.CurrentState}
		{
			case LOOT_MOVE
				Target id "${This.LootObject}"
				Move:ToTarget[5.000000]
				break
			case LOOT_START
				Target id "${This.LootObject}"
				EQExecute /loot
				This.LastLooted:Set[${This.LootObject}]
				This.LastAction:Set[${LavishScript.RunningTime}]
				break
			case LOOT_LOOT
				if ${This.LastLooted.Equal[NULL]}
				{
					This.LastLooted:Set[${Target.ID}]
				}
				if ${Cursor(exists)} || ${This.FullInventory} || ${Avatar.OnlyCash}
				{
					This.FullInventory:Set[TRUE]
					Scan:BlacklistID[${This.LastLooted}]
					Destroy
					This.DoneLooting:Set[TRUE]
					break
				}
				if ${Corpse.Items}
				{					
					Window[ConfirmationDialogBox].Child[Yes_Button]:LeftMouseUp
					if ${Corpse.Item[${This.LootIndex}].Name.NotEqual[NULL]}
					{
						This:EC["${Corpse.Item[${This.LootIndex}].Name}"]
						EQItemNotify loot${This.LootIndex} RightMouseUp
						This.LootIndex:Inc
					}
					Window[ConfirmationDialogBox].Child[Yes_Button]:LeftMouseUp
				}
				else
				{
					This.LootIndex:Set[1]
					Window[LootWnd].Child[DoneButton]:LeftMouseUp
				}
				This.LastAction:Set[${LavishScript.RunningTime}]
				break
			case LOOT_WAIT
				break
			case LOOT_DONE
				This:Reset
				This:ClearTarget
				break
		}
	}

	method AvatarLoot()
	{
		if ${UIElement[EQ Wanderbot].FindUsableChild[chkLoot,checkbox].Checked}
		{
			Avatar.Loot:Set[TRUE]
		}
		else
		{
			Avatar.Loot:Set[FALSE]
		}
	}

	method AvatarCash()
	{
		if ${UIElement[EQ Wanderbot].FindUsableChild[chkCash,checkbox].Checked}
		{
			Avatar.OnlyCash:Set[TRUE]
		}
		else
		{
			Avatar.OnlyCash:Set[FALSE]
		}
	}

	method AddJunkItem()
	{
		variable settingsetref junk
		variable string jitem
		junk:Set[${LavishSettings[EQ Wanderbot].FindSet[junk]}]
		jitem:Set[${UIElement[EQ Wanderbot].FindUsableChild[edtLoot,textentry].Text}]
		if !${junk.FindSetting[${jitem}]}
		{
			junk:AddSetting[${jitem},${jitem}]
			junk:Export[${Script[Wanderbot].CurrentDirectory}/Require/Settings/Junk.xml]
		}

		UIElement[EQ Wanderbot].FindUsableChild[edtLoot,textentry]:SetText[]
		This:UpdateJunk
	}

	method RemoveJunkItem()
	{
		variable settingsetref junk
		variable string jitem
		variable int a = 1
		variable int Total
		junk:Set[${LavishSettings[EQ Wanderbot].FindSet[junk]}]
		Total:Set[${UIElement[lstLoot@Loot@Pages@EQ Wanderbot].SelectedItems}]
		if ${Total}
		{
			do
			{
				jitem:Set[${UIElement[lstLoot@Loot@Pages@EQ Wanderbot].SelectedItem[${a}].Text}]
				junk.FindSetting[${jitem}]:Remove
				junk:Export[${Script[Wanderbot].CurrentDirectory}/Require/Settings/Junk.xml]
			}
			while ${a:Inc} <= ${Total}
		}
		This:UpdateJunk
	}

	method UpdateJunk()
	{
		variable settingsetref junk
		variable iterator settingIterator
		variable string jitem
		junk:Set[${LavishSettings[EQ Wanderbot].FindSet[junk]}]
		UIElement[lstLoot@Loot@Pages@EQ Wanderbot]:ClearItems
		
		junk:GetSettingIterator[settingIterator]
		if ${settingIterator:First(exists)}
		{
			do
			{
				UIElement[lstLoot@Loot@Pages@EQ Wanderbot]:AddItem[${settingIterator.Key},${settingIterator.Key},"ffaf0000"]
			}
			while ${settingIterator:Next(exists)}
		}
	}

	method Inject()
	{
		This:Reset

		LavishScript:RegisterEvent[LOOT_AVATAR_LOOT]
		LavishScript:RegisterEvent[LOOT_AVATAR_CASH]
		LavishScript:RegisterEvent[LOOT_JUNK_ADD]
		LavishScript:RegisterEvent[LOOT_JUNK_REMOVE]

		Event[LOOT_AVATAR_LOOT]:AttachAtom[${This.ObjectName}:AvatarLoot]
		Event[LOOT_AVATAR_CASH]:AttachAtom[${This.ObjectName}:AvatarCash]
		Event[LOOT_JUNK_ADD]:AttachAtom[${This.ObjectName}:AddJunkItem]
		Event[LOOT_JUNK_REMOVE]:AttachAtom[${This.ObjectName}:RemoveJunkItem]
	}
	 
	method Shutdown()
	{
		Event[LOOT_AVATAR_LOOT]:DetachAtom[${This.ObjectName}:AvatarLoot]
		Event[LOOT_AVATAR_CASH]:DetachAtom[${This.ObjectName}:AvatarCash]

		Event[LOOT_AVATAR_LOOT]:Unregister
		Event[LOOT_AVATAR_CASH]:Unregister
	}
}